package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.product.service.ICategoryService;
import com.atguigu.ssyx.product.service.ISkuInfoService;
import com.atguigu.ssyx.vo.product.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/admin/product/category")
@RequiredArgsConstructor
//@CrossOrigin
@Api(tags = "商品分类管理")
public class CategoryController {

    private final ICategoryService categoryService;
    private final ISkuInfoService skuInfoService;
   /* url: `${api_name}/${page}/${limit}`,
    method: 'get',
    params: searchObj*/


    @ApiOperation("分页查询商品分类列表")
    @GetMapping("/{page}/{limit}")

    public Result getCategory(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page,
                              @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                              @ApiParam(name = "categoryVo", value = "查询对象", required = false) CategoryVo categoryVo
    ) {
        //1.创建分页对象
        Page<Category> p = new Page<>(page, limit);
        //2.创建分页条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(categoryVo.getName() != null && !categoryVo.getName().isEmpty(), Category::getName, categoryVo.getName());
        categoryService.page(p, wrapper);
        return Result.ok(p);


    }

   /* url: `${api_name}/get/${id}`,
    method: 'get'*/

    @ApiOperation("根据id查询商品分类")
    @GetMapping("/get/{id}")
    public Result getCategory(@ApiParam(name = "id", value = "商品分类ID", required = true) @PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

/*    url: `${api_name}/save`,
    method: 'post',
    data: role*/


    @ApiOperation("添加商品分类")
    @PostMapping("/save")
    public Result saveCategory(@RequestBody Category category) {
        categoryService.save(category);
        return Result.ok(null);
    }


 /*   url: `${api_name}/update`,
    method: 'put',
    data: role*/

    @ApiOperation("修改商品分类")
    @PutMapping("/update")
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.ok(null);
    }


    /*
        url: `${api_name}/remove/${id}`,
        method: 'delete'*/
    @ApiOperation("根据Id删除商品分类")
    @DeleteMapping("/remove/{id}")
    public Result deleteCategory(@ApiParam(name = "id", value = "商品分类ID", required = true) @PathVariable Long id) {
        //根据分类id查询出是否有没有商品，如果有就不能删除分类，先删除商品才能进行删除分类
        countSku(id);
        return Result.ok(null);


    }

    private void countSku(Long id) {
        LambdaQueryWrapper<SkuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuInfo::getCategoryId, id);
        //根据分类id查询出是否有没有商品，如果有就不能删除分类，先删除商品才能进行删除分类
        int count = skuInfoService.count(wrapper);
        if (count < 0) {

            categoryService.removeById(id);
        } else {
            throw new SsyxException(ResultCodeEnum.CATEGROY_SKU_EXIST);
        }

    }
  /*  url: `${api_name}/batchRemove`,
    method: 'delete',
    data: idList*/

    @ApiOperation("批量删除商品分类")
    @DeleteMapping("/batchRemove")
    public Result batchDeleteCategory(@RequestBody List<Long> ids) {

        for (Long id : ids) {
            //根据分类id查询出是否有没有商品，如果有就不能删除分类，先删除商品才能进行删除分类
            countSku(id);
        }


        return Result.ok(null);
    }

   /* url: `${api_name}/findAllList`,
    method: 'get'*/

    @ApiOperation("获取全部商品分类")
    @GetMapping("/findAllList")
    public Result findAllList() {


        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(wrapper);
        return Result.ok(list);


    }


}